<?php
class QuoteModel extends BaseModel
{
	var $table  = 'quote';
	var $prikey = 'quote_id';
	var $_name  = 'quote';
	var $_relation = array(
		'belongs_order' => array(
				'model' => 'order',
				'type' => BELONGS_TO,
				'foreign_key' => 'order_id',
				'reverse'  => 'has_quote',
		),
		'belongs_sample' => array(
				'model' => 'sample',
				'type' => BELONGS_TO,
				'foreign_key' => 'sample_id',
				'reverse'  => 'has_quote',
		),
		'belongs_factory' => array(
				'model' => 'factory_info',
				'type' => BELONGS_TO,
				'foreign_key' => 'factory_id',
				'reverse'       => 'has_quote',
		)
	);
	
	function get_quote_list($conditions,$order,$limit){
		$member_mod    =& m('member');
		$factory_mod =& m('factory');
		$sample_mod=& m('sample');
		
		$fields = "sample.sample_id,sample.sample_sn,sample.ref_sample_sn,brand.brand_name,member.user_name,mem.user_name as member_name,sample.factory_price,sample.order_sn,sample.status,sourcing_sn,sourcing_status,sourcing.add_time,if_print";
		$tables = "{$this->table} sourcing " .
		"LEFT JOIN {$sample_mod->table} sample ON sample.sample_id=sourcing.sample_id " .
		"LEFT JOIN {$brand_mod->table} brand ON brand.brand_id = sample.brand_id " .
		"LEFT JOIN {$member_mod->table} member ON member.user_id = sample.merchandiser_id " .
		"LEFT JOIN {$member_mod->table} mem ON mem.user_id = sample.buyer_id ";
		
		/* 条件(WHERE) */
		$conditions = $this->_getConditions($conditions, true);
		/* 排序(ORDER BY) */
		if ($order)
		{
			$order = ' ORDER BY ' . $this->getRealFields($order);
		}
		
		/* 分页(LIMIT) */
		$limit && $limit = ' LIMIT ' . $limit;
		if ($limit)
		{
			$this->_updateLastQueryCount("SELECT COUNT(*) as c FROM {$tables}{$conditions}");
		}
		
		/* 完整的SQL */
		$sql = "SELECT {$fields} FROM {$tables}{$conditions}{$order}{$limit}";
		
		$sour_list =$this->db->getAll($sql);
		return $sour_list;
	}
}